import { param2Obj, apiData } from '@/utils'
import Mock from 'mockjs'
const Random = Mock.Random

const userMap = {
  admin: {
    roles: ['admin'],
    token: 'admin',
    introduction: '我是超级管理员',
    avatar: Random.image('200x200', '#50B347', '#FFF', 'Admin'),
    name: 'Super Admin'
  },
  editor: {
    roles: ['editor'],
    token: 'editor',
    introduction: '我是编辑',
    avatar: Random.image('200x200', '#FF6600', '#FFF', 'Editor'),
    name: 'Normal Editor'
  }
}

export default {
  loginByUsername: config => {
    const { username, password } = JSON.parse(config.body)
    if (!userMap[username] && username !== password) {
      return apiData('201', '用户名或密码错误')
    }

    return apiData('', '登录成功', userMap[username])
  },
  getUserInfo: config => {
    const { token } = param2Obj(config.url)
    if (userMap[token]) {
      return apiData('', 'User Info', userMap[token])
    } else {
      return apiData('201', 'Get user info error')
    }
  },
  logout: () => {
    return apiData('', 'Login Out')
  }
}
